﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Mvc.XForms.UI {
	public static class IPagedExtensions {
		public static int GetPageCount(this IPaged paged) {
			var pageCount = (int)Math.Ceiling((double)paged.TotalCount / (double)paged.PageSize);
			if (pageCount == 0)
				pageCount = 1;
			return pageCount;
		}

		public static bool GetIsPrevious(this IPaged paged) {
			return paged.PageIndex > 1;
		}

		public static bool GetIsNext(this IPaged paged) {
			return paged.PageCount > paged.PageIndex;
		}

		public static int GetValidPageIndex(this IPaged paged, int pageIndex) {
			if (paged.PageIndex > paged.PageCount)
				return paged.PageCount;
			
			if (paged.PageIndex < 1)
				return 1;

			return paged.PageIndex;
		}
	}
}